package com.ruoyi.project.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.common.StaticMethod;
import com.ruoyi.project.domain.FinRecordVoucher;
import com.ruoyi.project.domain.FinSubject;
import com.ruoyi.project.service.IFinSubjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.SecurityUtils;
import javax.annotation.Resource;
import com.ruoyi.project.mapper.FinRecordVoucherItemMapper;
import com.ruoyi.project.domain.FinRecordVoucherItem;
import com.ruoyi.project.service.IFinRecordVoucherItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 凭证项Service业务层处理
 *
 * @author abel
 * @date 2024-10-15
 */
@Service
public class FinRecordVoucherItemServiceImpl implements IFinRecordVoucherItemService
{
    private static Logger logger = LoggerFactory.getLogger(FinRecordVoucherItemServiceImpl.class.getName());

    @Resource
    private FinRecordVoucherItemMapper finRecordVoucherItemMapper;
    @Resource
    private IFinSubjectService finSubjectService;
    /**
     * 查询凭证项
     *
     * @param id 凭证项ID
     * @return 凭证项
     */
    @Override
    public FinRecordVoucherItem selectFinRecordVoucherItemById(Long id)
    {
        return finRecordVoucherItemMapper.selectById(id);
    }

    /**
     * 获取凭证项，获取不到数据抛错
     *
     * @param id 凭证项ID
     * @return 凭证项
     */
    @Override
    public FinRecordVoucherItem getFinRecordVoucherItemById(Long id)
    {
        FinRecordVoucherItem finRecordVoucherItem = finRecordVoucherItemMapper.selectById(id);
        if(finRecordVoucherItem == null){
            throw new ServiceException("凭证项id不正确，没有找到相关数据！");
        }
        return finRecordVoucherItem;
    }

    /**
     * 查询凭证项列表
     *
     * @param finRecordVoucherItem 凭证项
     * @return 凭证项
     */
    @Override
    public List<FinRecordVoucherItem> selectFinRecordVoucherItemList(FinRecordVoucherItem finRecordVoucherItem)
    {
        return finRecordVoucherItemMapper.selectFinRecordVoucherItemList(finRecordVoucherItem);
    }

    /**
     * 新增凭证项
     *
     * @param finRecordVoucherItem 凭证项
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFinRecordVoucherItem(FinRecordVoucherItem finRecordVoucherItem)
    {
        finRecordVoucherItem.setCreateTime(DateUtils.getNowDate());
        finRecordVoucherItem.setCreateBy(SecurityUtils.getUsername());
        finRecordVoucherItem.setCreateId(SecurityUtils.getUserId());;
        finRecordVoucherItem.setUpdateBy(SecurityUtils.getUsername());
        finRecordVoucherItem.setUpdateId(SecurityUtils.getUserId());
        finRecordVoucherItem.setUpdateTime(DateUtils.getNowDate());
        return finRecordVoucherItemMapper.insert(finRecordVoucherItem);
    }

    /**
     * 修改凭证项
     *
     * @param finRecordVoucherItem 凭证项
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFinRecordVoucherItem(FinRecordVoucherItem finRecordVoucherItem)
    {
        finRecordVoucherItem.setUpdateBy(SecurityUtils.getUsername());
        finRecordVoucherItem.setUpdateId(SecurityUtils.getUserId());
        finRecordVoucherItem.setUpdateTime(DateUtils.getNowDate());
        return finRecordVoucherItemMapper.updateById(finRecordVoucherItem);
    }

    /**
     * 批量删除凭证项
     *
     * @param ids 需要删除的凭证项ID
     * @return 结果
     */
    @Override
    public int deleteFinRecordVoucherItemByIds(Long[] ids)
    {
        for(Long id:ids){
            deleteFinRecordVoucherItemById(id);
        }
        return ids.length;
    }

    /**
     * 删除凭证项信息
     *
     * @param id 凭证项ID
     * @return 结果
     */
    @Override
    public int deleteFinRecordVoucherItemById(Long id)
    {
        return finRecordVoucherItemMapper.deleteFinRecordVoucherItemById(id);
    }

    @Transactional
    @Override
    public void createByFinRecord(FinRecordVoucher finRecordVoucher) {
        if(finRecordVoucher.getItemList()!=null && !finRecordVoucher.getItemList().isEmpty()){
            BigDecimal jieAmount = BigDecimal.ZERO;
            BigDecimal daiAmount = BigDecimal.ZERO;
            for(FinRecordVoucherItem voucherItem:finRecordVoucher.getItemList()){
                if(voucherItem.getSubjectId()==null){
                    logger.warn("subject id is null-->");
                    continue;
                }
                setCommonField(finRecordVoucher,voucherItem);
                if(voucherItem.getJieAmount()!=null){
                    jieAmount = jieAmount.add(voucherItem.getJieAmount());
                }
                if(voucherItem.getDaiAmount()!=null){
                    daiAmount = daiAmount.add(voucherItem.getDaiAmount());
                }
                finRecordVoucherItemMapper.insert(voucherItem);
            }
            if(!jieAmount.equals(daiAmount)){
                throw new ServiceException("保存失败，借方金额不等于贷方金额！请用检查后重试！");
            }
        }
    }

    @Transactional
    @Override
    public void updateByFinRecord(FinRecordVoucher finRecordVoucher) {
        finRecordVoucherItemMapper.deleteByRecordId(finRecordVoucher.getId());
        createByFinRecord(finRecordVoucher);
    }

    @Override
    public List<FinRecordVoucherItem> listByRecordId(Long recordId) {
        return finRecordVoucherItemMapper.listByRecordId(recordId);
    }

    @Override
    public void deleteByRecordId(Long recordId) {
        finRecordVoucherItemMapper.deleteByRecordId(recordId);
    }

    private void setCommonField(FinRecordVoucher voucher, FinRecordVoucherItem voucherItem){
        StaticMethod.setCreateField(voucherItem,SecurityUtils.getUserId(),SecurityUtils.getUsername());
        voucherItem.setRecordNo(voucher.getRecordNo());
        FinSubject finSubject = finSubjectService.getFinSubjectById(voucherItem.getSubjectId());
        voucherItem.setDirectionType(finSubject.getDirectionType());
        voucherItem.setSubjectName(finSubject.getName());
        voucherItem.setSubjectCode(finSubject.getCode());
        voucherItem.setSubjectParentId(finSubject.getParentId());
        voucherItem.setSubjectParentName(finSubject.getParentName());
        voucherItem.setSubjectParentCode(finSubject.getParentCode());
        voucherItem.setMonthValue(voucher.getMonthValue());
        voucherItem.setYearValue(voucher.getYearValue());
        voucherItem.setYearAndMonth(StaticMethod.genYearAndMonth(voucher.getYearValue(),voucher.getMonthValue()));
        voucherItem.setRecordId(voucher.getId());
        voucherItem.setRecordDate(voucher.getRecordDate());
        voucherItem.setPackageId(voucher.getPackageId());
    }

}
